package model.pieces;

import java.util.HashMap;

/**
 * Class implements a Factory Design Pattern. Used to create piece object
 */
public class PieceFactory {
	
	/**
	 * Interface of factory. Each factory of piece's derived type should implement it
	 */
	private interface PieceFac {
		public Piece create(boolean isFirstPlayer);
	}
	
	/** 
	 * Class used to create a Pawn with given parameter
	 */
	private class PawnFactory implements PieceFac {
		
		/**
		 * Returns a new Pawn assigned to given player 
		 */
		public Piece create(boolean isFirstPlayer) {
			
			// create a new Pawn with given parameter and return it
			return new Pawn(isFirstPlayer);
		}
	}
	
	/** 
	 * Class used to create a Knight with given parameter
	 */
	private class KnightFactory implements PieceFac {

		/**
		 * Returns a new Knight assigned to given player 
		 */
		public Piece create(boolean isFirstPlayer) {
			 			
			// create a new Knight with given parameter and return it
			return new Knight(isFirstPlayer);						
		}
	}
	
	/** 
	 * Class used to create a Bishop with given parameter
	 */
	private class BishopFactory implements PieceFac {
		
		/**
		 * Returns a new Bishop assigned to given player
		 */
		public Piece create(boolean isFirstPlayer) {
			
			// create a new Bishop with given parameter and return it
			return new Bishop(isFirstPlayer);
		}
	}
	
	/** 
	 * Class used to create a Rook with given parameter
	 */
	private class RookFactory implements PieceFac {
		
		/**
		 * Returns a new Rook assigned to given player
		 */
		public Piece create(boolean isFirstPlayer) {
			
			// create a new Rook with given parameter and return it
			return new Rook(isFirstPlayer);
		}
	}
	
	/** 
	 * Class used to create a Queen with given parameter
	 */
	private class QueenFactory implements PieceFac {
		
		/**
		 * Returns a new Queen assigned to given player
		 */
		public Piece create(boolean isFirstPlayer) {
			
			// create a new Queen with given parameter and return it
			return new Queen(isFirstPlayer);
		}
	}
	
	/** 
	 * Class used to create a King with given parameter
	 */
	private class KingFactory implements PieceFac {
		
		/**
		 * Returns a new King assigned to given player
		 */
		public Piece create(boolean isFirstPlayer) {
			
			// create a new King with given parameter and return it
			return new King(isFirstPlayer);
		}
	}
	
	// member
	
	private HashMap<String, PieceFac> pieceFactory; // used to activate create method of correct type
	
	/**
	 * Constructor- creates a new piece factory
	 */
	public PieceFactory() {
		
		// allocate a new map of piece factories
		pieceFactory = new HashMap<String, PieceFac>();
		// set Pawn factory
		pieceFactory.put("Pawn", new PawnFactory());
		// set Knight factory
		pieceFactory.put("Knight", new KnightFactory());
		// set Bishop factory
		pieceFactory.put("Bishop", new BishopFactory());
		// set Rook factory
		pieceFactory.put("Rook", new RookFactory());
		// set Queen factory
		pieceFactory.put("Queen", new QueenFactory());
		// set King factory
		pieceFactory.put("King", new KingFactory());
	}
		
	/**
	 * Gets piece type and parameters and creates a new piece accordingly 
	 * @param type- type for new piece
	 * @param row- row index for new type
	 * @param col- column index for new type
	 * @param isFirstPlayer- indicates if piece belongs to first player or not
	 * @return
	 */
	public Piece createPiece(String type, boolean isFirstPlayer) {
		return pieceFactory.get(type).create(isFirstPlayer);
	}
}